package com.noble.meether.chat.ui.agora.act;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.permission.PermissionUtil;
import com.noble.library.permission.callback.PermissionListener;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.RtcUIShow;
import com.noble.meether.chat.ui.agora.act.base.RtcBaseActivity;
import com.noble.meether.chat.ui.agora.act.voice.girlver.RtcInitFootPanel;
import com.noble.meether.chat.ui.agora.model.AgoraConstant;
import com.noble.meether.chat.ui.agora.model.EngineConfig;
import com.noble.meether.chat.ui.agora.util.RtcSubUtil;
import com.noble.meether.chat.ui.agora.util.RtcUtil;
import com.noble.meether.fragment.rob.RobMessage;

import java.util.ArrayList;
import java.util.Random;

/**
 * 男性： 抢聊大厅发起聊天
 */
public class RtcGroupInitAct extends RtcBaseActivity implements PObserver, RequestComplete {
    private ImageView imgBg;
    private RtcInitFootPanel footPanel;

    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_group);
        initView();
    }

    private void initView() {
        RtcUtil.getInstance().resetSynchStatus();   // 重置挂断状态
        imgBg = findViewById(R.id.iv_bg);
        RelativeLayout footer = findViewById(R.id.ll_foot_panel);
        footPanel = new RtcInitFootPanel(this);
        footer.addView(footPanel.getContentView());

        RtcSubUtil.getInstance().playSound();  // 开启铃声
        ModuleMgr.getRtcChatMgr().reqGirlRand(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        permissionCheck();
    }

    private void permissionCheck() {
        PermissionUtil.checkAVPermission(this, new PermissionListener() {
            @Override
            public void success() {
                ModuleMgr.getRtcChatMgr().reqChatAdd(RtcGroupInitAct.this);
            }

            @Override
            public void fail() {
                PToast.showShort("授权失败，无法发起视频聊天");
                finish();
            }
        });
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGirlRand) {
            if (!response.isOk()) {
                return;
            }
            UserLightList list = (UserLightList) response.getBaseData();
            refreshView(list.getLightList());
            return;
        }

        if (response.getUrlParam() == UrlParam.reqChatAdd) {
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                finish();
                return;
            }
            MsgMgr.getInstance().attach(this); // 加入聊天池成功后，监听消息
            ModuleMgr.getRtcEnginMgr().releaseEngineConfig();  // 重置聊天参数
            RobMessage robMessage = new RobMessage();
            robMessage.parseJson(response.getResString());
            EngineConfig config = ModuleMgr.getRtcEnginMgr().getEngineConfig();
            config.isCalling = true;
            config.mChatType = AgoraConstant.RTC_CHAT_VIDEO;
            config.mChannel = robMessage.getcName();
            config.mVcid = robMessage.getVcId();
            config.mChannelKey = robMessage.getcKey();
        }
    }

    private void refreshView(final ArrayList<UserLight> list) {
        if (list == null || list.size() <= 0) return;
        int position = new Random().nextInt(list.size());
        ImageLoader.loadBlur(this, list.get(position).getAvatar(), imgBg, 8, R.drawable.sp_bk);
        if (footPanel != null) footPanel.refreshView(list.get(position));

        if (runnable == null) {
            runnable = () -> refreshView(list);
        }
        MsgMgr.getInstance().delay(this, runnable, 5000);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Rtc_Group_Chat_Msg:      // 男方收到女性抢聊消息
                startChat();
                break;
        }
    }

    private void startChat() {
        RtcUIShow.showRtcRobVideoAct(this);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        runnable = null;
        MsgMgr.getInstance().detach(this);
        RtcSubUtil.getInstance().stopPlay();
        ModuleMgr.getRtcEnginMgr().releaseLocalTimer();
        if (footPanel != null) footPanel.reqVCGroupCancel();  // 意外退出调用
    }
}
